package com.ruoyi.system.handler;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.GzdsGrade;
import com.ruoyi.system.domain.GzdsQuestionInfo;
import com.ruoyi.system.service.IGzdsGradeService;
import com.ruoyi.system.service.IGzdsQuestionInfoService;
import com.ruoyi.system.utils.QuestionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author sxctc
 * @title: GzdsGradeHandler
 * @projectName ruoyi
 * @description: 分数处理类
 * @date 2021/5/31 15:43
 */
@Component
public class GzdsGradeHandler {

    @Autowired
    private IGzdsGradeService gzdsGradeService;

    @Autowired
    private IGzdsQuestionInfoService gzdsQuestionInfoService;

    /**
     *
     * @param code
     * @param banklevel
     * @param questionInfoList
     * @return
     */
    public int addGrade(Long code, String banklevel, List<GzdsQuestionInfo> questionInfoList) throws ParseException {
        /** 待返回数据 **/
        int count = 0;

        Date endTime = new Date();

        int timeSum = 1800000;
        int countSum = 40;
        int timeCount = 30;
        if (StringUtils.equals("复赛", banklevel)) {
            timeSum = 1200000;
            countSum = 30;
            timeCount = 20;
        } else if (StringUtils.equals("决赛", banklevel)) {
            timeSum = 900000;
            countSum = 30;
            timeCount = 15;
        } else if (StringUtils.equals("加赛", banklevel)) {
            timeSum = 900000;
            countSum = 30;
            timeCount = 15;
        }

        /** 获取答题正确数 **/
        int truQuestion = this.getTrueQuestionSize(questionInfoList);

        GzdsGrade gzdsGrade = gzdsGradeService.selectGzdsGradeById(code);
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startTime = gzdsGrade.getStartTime();
        startTime = sd.parse(sd.format(startTime));
        endTime = sd.parse(sd.format(endTime));
        gzdsGrade.setEndTime(endTime);
        Calendar Startcalendar = Calendar.getInstance();
        Startcalendar.setTime(startTime);
        Calendar Endcalendar = Calendar.getInstance();
        Endcalendar.setTime(endTime);
        long Starttime = Startcalendar.getTimeInMillis();
        long Endtime = Endcalendar.getTimeInMillis();
        long between_days = Endtime - Starttime;
        int time = Integer.parseInt(String.valueOf(between_days));
        if(time>timeSum || time<0){
            time = timeSum;
        }
        float i = (1 - ((float) time / 1000) / (timeCount * 60)) * 30 + ((float) truQuestion / countSum) * 70;
        NumberFormat formatter = new DecimalFormat("0.00");
        String formmatedFloatValue = formatter.format(i);
        gzdsGrade.setPerformance(formmatedFloatValue);
        gzdsGrade.setBanklevel(banklevel);
        gzdsGrade.setTruequestion(truQuestion);
        count = gzdsGradeService.updateGzdsGrade(gzdsGrade);

        /** 返回数据 **/
        return count;
    }

    /**
     * 处理答题数据
     * @param questionInfoList
     * @return
     */
    private List<GzdsQuestionInfo> handlerQuestionList(List<GzdsQuestionInfo> questionInfoList) {
        /** 待返回数据 **/
        List<GzdsQuestionInfo> retList = new ArrayList<>();

        /** 存放答题数据(将多选题答案合并) **/
        Map<Long, String> tempMap = new HashMap<>();

        /** 循环遍历数据，处理多选题类型 **/
        if (questionInfoList!=null && questionInfoList.size()>0) {
            for (GzdsQuestionInfo info : questionInfoList) {
                if (!tempMap.containsKey(info.getQuestionId())) {
                    tempMap.put(info.getQuestionId(), info.getAnswer());
                } else {
                    tempMap.put(info.getQuestionId(), tempMap.get(info.getQuestionId()) + "/"+ info.getAnswer());
                }
            }

            for (Long id : tempMap.keySet()) {
                /* 封装数据 */
                GzdsQuestionInfo info = new GzdsQuestionInfo();
                info.setQuestionId(id);
                info.setAnswer(tempMap.get(id));

                retList.add(retList.size(), info);
            }
        }

        /** 返回数据 **/
        return retList;
    }

    /**
     * 获取答题正确数
     * @param questionInfoList
     * @return
     */
    private int getTrueQuestionSize(List<GzdsQuestionInfo> questionInfoList) {
        /** 待返回数据 **/
        int count = 0;

        /** 处理多选题数据 **/
        questionInfoList = this.handlerQuestionList(questionInfoList);

        if (questionInfoList!=null && questionInfoList.size()>30) {
            return count;
        }

        /** 循环遍历数据，获取答题正确数 **/
        if (questionInfoList!=null && questionInfoList.size()>0) {
            for(GzdsQuestionInfo info : questionInfoList) {
                GzdsQuestionInfo question = QuestionUtils.getQuestionCache(String.valueOf(info.getQuestionId()));
                if (question == null) {
                    question = gzdsQuestionInfoService.selectGzdsQuestionInfoById(info.getQuestionId());
                }

                if(question == null) {
                    continue;
                }

                /* 多选题 */
                if (StringUtils.equals("1", question.getQuestions())) {
                    if (this.checkAnswer1(question, info.getAnswer())) {
                        count ++;
                    }
                }
                /* 单选或判断题 */
                else {
                    if (StringUtils.equals(info.getAnswer(), question.getAnswer())) {
                        count ++;
                    }
                }
            }
        }

        /** 返回数据 **/
        return count;
    }

    /**
     * 判断多选题是否答对
     * @param question
     * @param answer
     * @return
     */
    private Boolean checkAnswer1(GzdsQuestionInfo question, String answer) {
        /** 待返回数据 **/
        Boolean flag = true;

        /** 处理数据 **/
        String[] questionArray = question.getAnswer().split("/");
        String[] answerArray = answer.split("/");
        if (questionArray==null || answerArray==null) {
            flag = false;
        } else {
            for (int i = 0; i < questionArray.length; i++) {
                if (!answer.contains(questionArray[i])) {
                    flag = false;
                    break;
                }
            }
            for (int i=0; i< answerArray.length; i++) {
                if (!question.getAnswer().contains(answerArray[i])) {
                    flag = false;
                    break;
                }
            }
        }

        /** 返回数据 **/
        return flag;
    }

    /**
     * 判断正确率是否大于50%
     * @param banklevel
     * @param truQuestion
     * @return
     */
    private Boolean checkTrueRate(String banklevel, int truQuestion) {
        /** 待返回数据 **/
        Boolean flag = true;

        if (StringUtils.equals("初赛", banklevel)) {
            if (truQuestion < 20) {
                flag = false;
            }
        } else if (StringUtils.equals("复赛", banklevel)) {
            if (truQuestion < 15) {
                flag = false;
            }
        } if (StringUtils.equals("决赛", banklevel)) {
            if (truQuestion < 15) {
                flag = false;
            }
        }

        /** 返回数据 **/
        return flag;
    }
}
